﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MuaBanLike.com
{
    public partial class MainWindow : Window
    {
        
        private bool onSkipFollowFaceBook()
        {
            var doc = (mshtml.HTMLDocument)MainBrowser.Document;

            mshtml.HTMLDivElement divTbl = (mshtml.HTMLDivElement)doc.getElementById("tbl");
            if (divTbl != null)
            {

                foreach (mshtml.HTMLDivElement item in divTbl.children)
                {
                    if (item != null)
                    {
                        if (item.style == null || item.style.cssText == null)
                        {
                            foreach (var itemLike in item.children)
                            {
                                mshtml.HTMLBlockElement centerTag = itemLike as mshtml.HTMLBlockElement;
                                if (centerTag != null)
                                {
                                    foreach (var itemFont in centerTag.children)
                                    {

                                        if (itemFont != null && itemFont.GetType().ToString() == "mshtml.HTMLFontElementClass")
                                        {
                                            foreach (var itemHref in itemFont.children)
                                            {
                                                if (itemHref.GetType().ToString() == "mshtml.HTMLAnchorElementClass")
                                                {
                                                    if (m_likeItem != null)
                                                    {
                                                        string msg = String.Format("Skip {0} : {1} coins", m_likeItem.LikelLink, m_likeItem.Coins);
                                                        showBallon(m_mainWeb.ToString(), msg);
                                                    }

                                                    ((mshtml.HTMLAnchorElement)itemHref).click();
                                                    return true;
                                                }
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            }

            return false;
        }

        public void onClosePopup()
        {
            if (thread != null)
                thread.CancelAsync();

            if (wnd != null)
            {
                wnd.Close();
                wnd = null;
            }

            //Wait end goto next item
            startDetectStatusThread();
        }

        private void onFollowFakceBook()
        {
            assignEventPopup();
            var doc = (mshtml.HTMLDocument)MainBrowser.Document;

            mshtml.HTMLDivElement divTbl = (mshtml.HTMLDivElement)doc.getElementById("tbl");
            if (divTbl != null)
            {

                foreach (mshtml.HTMLDivElement item in divTbl.children)
                {
                    if (item != null)
                    {
                        if (item.style == null || item.style.cssText == null)
                        {
                            foreach (var itemLike in item.children)
                            {
                                mshtml.HTMLBlockElement centerTag = itemLike as mshtml.HTMLBlockElement;
                                if (centerTag != null)
                                {
                                    foreach (var itemHref in centerTag.children)
                                    {
                                        if (itemHref.GetType().ToString() == "mshtml.HTMLAnchorElementClass")
                                        {

                                            int nId = Convert.ToInt32(item.id);
                                            string sInnerHtml = ((mshtml.HTMLAnchorElement)itemHref).outerHTML;
                                            string[] arr = sInnerHtml.Split(',');
                                            string sCoin = "0";
                                            try
                                            {
                                                sCoin = arr[5].Replace("'", "");
                                            }
                                            catch (Exception ex)
                                            {
                                            }

                                            int nCoin = Convert.ToInt32(sCoin);
                                            string sFace = arr[1].Replace("'", "");

                                            m_likeItem = new LikeItem(nId, nCoin, sFace);

                                            showBallon(m_mainWeb.ToString(), m_likeItem.LikelLink);
                                            ((mshtml.HTMLAnchorElement)itemHref).click();

                                        }
                                    }
                                }

                            }
                            break;
                        }
                    }
                }
            }



        }
    }
}
